﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.OutboundShipments;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Domain.Storages;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using System;
using System.Xml.Linq;
using T.E.ERP.Read.API.Applicantions.Command.SaleOrderInfoCommand;
using T.E.ERP.Read.API.Applicantions.Command.SaleOutRepositoryCommand;


namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SaleOutRepositoryCommandHandler
{
    public class GetSaleOutRepositoryListCommandHandler : IRequestHandler<GetSaleOutRepositoryListCommand, APIResult<List<View_SaleOutRepository>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BaseStorage> storeBiz;
        private readonly IBaseRepository<SaleOutRepository> saleOutBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public GetSaleOutRepositoryListCommandHandler(IMapper mapper, IBaseRepository<BaseStorage> storeBiz, IBaseRepository<SaleOutRepository> saleOutBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.storeBiz = storeBiz;
            this.saleOutBiz = saleOutBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;
        }

        public Task<APIResult<List<View_SaleOutRepository>>> Handle(GetSaleOutRepositoryListCommand request, CancellationToken cancellationToken)
        {
            var store = storeBiz.GetAll().Result;
            var saleOut = saleOutBiz.GetAll().Result;
            var orderMaterials = orderMaterialsBiz.Quary(x => x.MaterialsClass == 3).Result;
            var list = from storeT in store
                       join saleOutT in saleOut
                       on storeT.Cid equals saleOutT.Repository
                       join orderMaterialsT in orderMaterials
                       on saleOutT.Id equals orderMaterialsT.SaleCode
                       select new { storeT, saleOutT, orderMaterialsT };
            if (request.Id.IsNotNull())
            {
                list = list.Where(x => x.saleOutT.Id.ToString() == request.Id);
            }
            if (request.DocumentStatus != 0)
            {
                list = list.Where(x => x.saleOutT.DocumentStatus == request.DocumentStatus);
            }
            if (request.BusinessStatus != 0)
            {
                list = list.Where(x => x.saleOutT.BusinessStatus == request.BusinessStatus);
            }
            if (request.Repository != 0)
            {
                list = list.Where(x => x.saleOutT.Repository == request.Repository);
            }
            APIResult<List<View_SaleOutRepository>> result = new APIResult<List<View_SaleOutRepository>>();
            result.Count = list.Count();
            result.data = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).OrderByDescending(a => a.saleOutT.DocumentDate).Select(c =>
            new View_SaleOutRepository
            {
                //单据编码
                Id = c.saleOutT.Id,
                //业务类型
                BusinessType = c.saleOutT.BusinessType,
                //业务状态
                BusinessStatus = c.saleOutT.BusinessStatus,
                //单据类型
                DocumentType = c.saleOutT.DocumentType,
                //单据状态
                DocumentStatus = c.saleOutT.DocumentStatus,
                //单据日期
                DocumentDate = c.saleOutT.DocumentDate.DateTimeToString("yyyy-MM-dd HH:mm:ss","--"),
                //审批人姓名
                ExamUserName = c.saleOutT.ExamUserName,
                //物料名称
                MaterialsName = c.orderMaterialsT.MaterialsName,
                //物料编码
                MaterialsCode = c.orderMaterialsT.MaterialsCode,
                //物料id
                MaterialsId = c.orderMaterialsT.Id,
                //数量
                CalulateNumber = c.orderMaterialsT.CalulateNumber,
                //单价
                Price = c.orderMaterialsT.Price,
                //金额
                TotalPrice = c.orderMaterialsT.TotalPrice,
                //仓库id
                Repository = c.storeT.Cid,
                //仓库代码
                BaseCode = c.storeT.BaseCode,
                BaseName = c.storeT.BaseName,

            }).ToList();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.成功.ToString();
            return Task.FromResult(result);
        }
    }
}
